var ValidateShops = function () {
	return {
		init:function() {
			jQuery.validator.addMethod("regex",  
				function(value, element, params) {  
					var exp = new RegExp(params);  
					return exp.test(value);  
				}, "格式错误");
			validator =$('.form-horizontal').validate({
				errorElement: 'span', //default input error message container
				errorClass: 'help-block', // default input error message class
				focusInvalid: false, // do not focus the last invalid input
				rules: {
					loginName: {
						required: true,
						regex:"^\\w+$",
						remote:{  
							type: "post",  
							url: ajaxUrl,  
							data: {  
								loginName: function() {  
									return $("#loginName").val();  
								},
								ajax:"checkUser"
							},  
							dataType: "json"
						}  
					},
					loginPwd: {
						required: true,
						rangelength:[5,16] 
					},
					reLoginPwd: {
						required: true,
						rangelength:[5,16] ,
						equalTo:"#loginPwd"
					},
					shopName: {
						required: true,
						remote:{  
							type: "post",  
							url: ajaxUrl,  
							data: {  
								shopName: function() {  
									return $("#shopName").val();  
								},
								ajax:"checkShop"
							},  
							dataType: "json"
						}  
					},
					userPhone: {
						required: true,
						rangelength:[11,11] ,
						remote:{  
							type: "post",  
							url: ajaxUrl,  
							data: {  
								shopName: function() {  
									return $("#userPhone").val();  
								},
								ajax:"checkPhone"
							},  
							dataType: "json"
						}  
					},
					shopSn: {
						required: true,
						maxlength:20
					},
					shopTel: {
						required: true
					},
					shopAddress: {
						required: true
					},
					areaId3: {
						required: true,
						min:1
					},
					userName: {
						required: true,
						maxlength:20
					},
					bankNo: {
						required: true,
						rangelength:[16,19]
					},
					goodsCatId1: {
						required: true,
						min:1
						
					},
					bankId: {
						required: true,
						min:1
					},
					
				},

				messages: {
					loginName: {
						required: "用户名不能为空。",
						regex:"用户名只能用数字字母下划线。",
						remote:"用户名已占用！"
					},
					loginPwd: {
						required: "密码不能为空。",
						rangelength: "密码最少5个字符最多16个字符。"
					},
					reLoginPwd: {
						required: "密码不能为空。",
						rangelength: "密码最少5个字符最多16个字符。",
						equalTo:"请输入相同的密码！"
					},
					shopName: {
						required: "店铺名称不能为空。",
						remote:"店铺名已占用！"
					},
					userPhone: {
						required: "手机号码不能为空。",
						rangelength:"手机号码格式不正确。" ,
						remote:"手机号已被使用"
					},
					shopSn: {
						required: "店铺编号不能为空。",
						maxlength:"店铺编号格式不正确。"
					},
					shopTel: {
						required: "店铺电话不能为空。"
						
					},
					shopAddress: {
						required: "店铺地址不能为空。"
						
					},
					areaId3: {
						required: "请选择地区。",
						min:"请选择地区。"
					},
					userName: {
						required: "店主名称不能为空。",
						maxlength:"店主名称格式不正确。"
					},
					bankNo: {
						required: "银行卡号码不能为空。",
						rangelength:"银行卡号码格式不正确。" 
					},
					goodsCatId1: {
						required: "请选择所属行业。",
						min: "请选择所属行业。"
					},
					bankId: {
						required: "请选择绑定银行。",
						min:"请选择绑定银行。" 
					},
				},

				invalidHandler: function (event, validator) { //display error alert on form submit
					$('.alert-danger', $('.manager-form')).show();
				},

				highlight: function (element) { // hightlight error inputs
					$(element)
						.closest('.form-group').addClass('has-error'); // set error class to the control group
				},

				success: function (label) {
					label.closest('.form-group').removeClass('has-error');
					label.remove();
				},

				errorPlacement: function (error, element) {
					error.insertAfter(element.closest(':input'));
					
				},

				submitHandler: function (form) {
					form.submit();
					
				}
			});
			$('.form-horizontal input').keypress(function (e) {
				if (e.which == 13) {
					if ($('.form-horizontal').validate().form()) {
						$('.form-horizontal').submit();
					}
					return false;
				}
			});
			if($('#shopId').val()){
				$("#userPhone").rules('remove','remote');
				$("#shopName").rules('remove','remote');
			}
			
			$('.timepicker').timepicker({autoclose: true,minuteStep: 30,showMeridian: false});
			$('.timepicker').next('.input-group-btn').click(function(e){
				e.preventDefault();
				$(this).prev('.timepicker').timepicker('showWidget');
			});
			

			
			loadData($("#areaId1"));

			//绑定事件
			$("#areaId1,#areaId2").change(function(){
				var id = $(this).val();
				loadData($(this).parent().next().children('select'), id);
			});

			
			var shopMap = null;
			var toolBar = null;
			ShopMapInit();
			
			$("input[name='shopStatus']").change(function(){
				if ($(this).val()<=-1) { //选择拒绝的时候
					$('.refuse').show();
					
				}else{ //恢复验证
					$('.refuse').hide();
				}
			});	
			$("input[name='isInvoice']").change(function(){
				if ($(this).val()==1) { //选择发票的时候
					$('.invoice').show();						
				}else{ 
					$('.invoice').hide();
				}
			});

			
			function ShopMapInit(option){
			   var opts = {zoom:$('#mapLevel').val(),longitude:$('#longitude').val(),latitude:$('#latitude').val()};
			   if(shopMap)return;
			   $('#shopMap').show();
			   shopMap = new AMap.Map('aMap', {
					view: new AMap.View2D({
						zoom:opts.zoom
					})
			   });
			   if(opts.longitude!='' && opts.latitude!=''){
				   shopMap.setZoomAndCenter(opts.zoom, new AMap.LngLat(opts.longitude, opts.latitude));
				   var marker = new AMap.Marker({
						position: new AMap.LngLat(opts.longitude, opts.latitude), //基点位置
						icon:"http://webapi.amap.com/images/marker_sprite.png"
				   });
				   marker.setMap(shopMap);
			   }
			   AMap.plugin(["AMap.ToolBar"],function(){		
					toolBar = new AMap.ToolBar();
					shopMap.addControl(toolBar);	
					toolBar.show();
			   });
			   //设置城市
				AMap.event.addDomListener(document.getElementById('areaId2'), 'change', function() {
					var cityName = $('#areaId2').find("option:selected").html();
					if (!cityName) {
						cityName = $('#areaId1').find("option:selected").html();
					}
					shopMap.setCity(cityName);
				});
				
				AMap.event.addDomListener(document.getElementById('areaId1'), 'change', function() {
					var cityName = $('#areaId1').find("option:selected").html();
					if (!cityName) {
						cityName = $('#areaId1').find("option:selected").html();
					}
					shopMap.setCity(cityName);
				});
			   
			   AMap.event.addListener(shopMap,'click',function(e){
					shopMap.clearMap();
					$('#longitude').val(e.lnglat.getLng());
					$('#latitude').val(e.lnglat.getLat());
					var marker = new AMap.Marker({
						position: e.lnglat, //基点位置
						icon:"http://webapi.amap.com/images/marker_sprite.png"
					});
					marker.setMap(shopMap);
			   });
			   AMap.event.addListener(shopMap,'zoomchange',function(e){
				   $('#mapLevel').val(this.getZoom());
			   })
			}
		   //ajax 加载数据的方法
			function loadData(select, id){
				if (id === undefined) {
					id = 0;
				}
				$.ajax({
					url:ajaxUrl,
					type:'get',
					data:{id:id,ajax:'getRegion'},
					dataType:'json',
					success:function(data){
						select.nextAll().andSelf().empty().hide();	//清空下一个select并隐藏
						if (data.length > 0) {
							select.show();
							$.each(data, function(index, value) {
								if(value.id==areaId1 || value.id==areaId2 || value.id==areaId3 ){
									$('<option>').html(value.name).val(value.id).attr('selected',true).appendTo(select);
								}else{
									$('<option>').html(value.name).val(value.id).appendTo(select);
								}
							});
							select.trigger('change');		//触发下一个select的change事件
						}				
					}
				});
			}
		   		   
		}
    };
}();

